package com.spider.silence.worker;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.spider.silence.downloader.TianFuForumDownLoader;
import com.spider.silence.itf.output.Writer;
import com.spider.silence.output.*;
import com.spider.silence.pool.TianFuForumDownLoaderPool;
import com.spider.silence.task.TianFuForumTask;
import com.spider.silence.task.manager.CommonTaskManager;
import com.spider.silence.utils.DateUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/30 0030.
 */
public class TianFuForumWorker implements  Runnable {
    private Logger logger = Logger.getLogger(TianFuForumWorker.class);
    @Override
    public void run() {

        while (true&&!Thread.interrupted()){
            TianFuForumDownLoaderPool pool=TianFuForumDownLoaderPool.getInstance();
            TianFuForumDownLoader downLoader =null;
            FileWriter writer = null;
            FileWriterPool writerPool =FileWriterPool.getInstance();
            try {
                downLoader = pool.fetchDownLoader(1000);
                if(downLoader!=null){
                    TianFuForumTask task = (TianFuForumTask) CommonTaskManager. takeTask("TianFuForum",1000);
                    if(task == null){
                        continue;
                    }
                    JSONObject content = downLoader.crawlItem(task.getForumUrl());

                    JSONObject data=content.getJSONObject("data");
                    System.out.println("data-->"+data);
                    if(data != null && !"".equals(data)&&!data.isEmpty()){
                        String filePrefix="TianFuForum";//文件前缀
                        long tsp= DateUtils.dateToTimestamp( DateUtils.parseDateTime(data.getString("publish_time"))).getTime();
                        String user_id=data.getString("publish_user_id");
                        String tid=data.getString("id");
                        //Writer writer =new FTPWriter();
                        String fileName=filePrefix+"_"+tsp+"_"+user_id+"_"+tid;//平台名称（或者英文）_ 言论发布的时间戳 _ 发布用户的ID _ 言论的tid
                        String publishDate=DateUtils.formatDate(DateUtils.parseDate(data.getString("publish_time"))).replace("-","-");
                        writer= writerPool.fetchWriter();
                        Map<String,File> map=downLoader.getFiles();
                        writer.write("天府论坛_speeches_"+publishDate,fileName,JSON.toJSONString(content,SerializerFeature.BrowserCompatible),map);
                    }
                }

            } catch (InterruptedException e) {
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            } catch (Exception e) {
                logger.error(e.getMessage(),e);
                e.printStackTrace();
            }finally {
                if(downLoader!=null) {
                    pool.releaseDownLoader(downLoader);
                }
                if(writer!=null) {
                    writerPool.releaseWriter(writer);
                }
            }
        }
    }
}
